home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / SDKs / Word Services SDK 1.0.6 / Writeswell Jr 1.2.1 Sources ƒ / Writeswell Jr. Source / AEObj.c next >
Encoding:
C/C++ Source or Header  |  1995-10-14  |  7.9 KB  |  307 lines  |  [TEXT/MMCC]

  1. /* AEObj.c
  2.  * Utilities for dealing with Apple Event Objects (making descriptors, etc.)
  3.  * ©1992 Working Software, Inc.
  4.  * This source code is copyrighted.  Permission is granted to use the Word Services
  5.  * portion of the Writeswell Jr. source code in your own programs, but you 
  6.  * may not distribute the Writeswell Jr. word-processor code as a 
  7.  * commercial product.  If you modify the code, please do not call it 
  8.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  9.  * program and don’t have to deal with a number of different versions with 
  10.  * who-knows-what going on in the code.
  11.  * 
  12.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  13.  * 18 Dec 91 Mike Crawford
  14.  */
  15.  
  16. #include <AppleEvents.h>
  17. #include <AEObjects.h>
  18. #include <AERegistry.h>
  19. #include "TBConstants.h"
  20. #include "TBGlobals.h"
  21. #include "AEObj.h"
  22. #include "ObWind.h"
  23. #include "ObText.h"
  24. #include "ObOspec.h"
  25. #include "Gripe.h"
  26.  
  27. OSErr InstallMyAccessors( void );
  28. pascal OSErr MyCountProc(DescType desiredType,
  29.                         DescType containerClass,
  30.                         const AEDesc *container,
  31.                         long *result);
  32. pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken);
  33. OSErr InstallMyCoercers( void );
  34. extern pascal OSErr AEObjectInit(void);
  35.  
  36. // OSL Callbacks
  37. static OSLCountUPP gCountUPP = (OSLCountUPP)NULL; 
  38. static OSLDisposeTokenUPP gDisposeTokenUPP = (OSLDisposeTokenUPP)NULL;
  39.  
  40. // Object Accessors
  41. static OSLAccessorUPP gWindFromNullUPP = (OSLAccessorUPP)NULL;
  42. static OSLAccessorUPP gPropFromWindUPP = (OSLAccessorUPP)NULL;
  43. static OSLAccessorUPP gTextFromWindUPP = (OSLAccessorUPP)NULL;
  44. static OSLAccessorUPP gWordFromTETextUPP = (OSLAccessorUPP)NULL;
  45. static OSLAccessorUPP gCharFromTETextUPP = (OSLAccessorUPP)NULL;
  46. static OSLAccessorUPP gPropFromTETextUPP = (OSLAccessorUPP)NULL;
  47. static OSLAccessorUPP gOspecFromWindUPP = (OSLAccessorUPP)NULL;        // Special for Word Services
  48.  
  49. // Coercion Handlers
  50. static AECoercePtrUPP gTextPtrToPStringUPP = (AECoercePtrUPP)NULL;
  51.  
  52. OSErr InitAEObjStuff( void )
  53. {
  54.     OSErr err;
  55.     
  56.     err = AEObjectInit();
  57.     if ( err ){
  58.         Gripe( "\pAEObjectInit failed" );
  59.         return err;
  60.     }
  61.     
  62.     err = InstallMyAccessors();
  63.     if ( err ){
  64.         Gripe( "\pInstallMyAccessors failed" );
  65.         return err;
  66.     }
  67.  
  68.     gCountUPP = NewOSLCountProc( MyCountProc );
  69.     if ( !gCountUPP )
  70.         return memFullErr;
  71.  
  72.     gDisposeTokenUPP = NewOSLDisposeTokenProc( MyDisposeTokenProc );
  73.     if ( !gDisposeTokenUPP )
  74.         return memFullErr;
  75.  
  76.     /* Install the callback functions used by the AEObj parser */
  77.     err = AESetObjectCallbacks((OSLCompareUPP)nil, 
  78.             (OSLCountUPP)gCountUPP, 
  79.             (OSLDisposeTokenUPP)gDisposeTokenUPP,
  80.             (OSLGetMarkTokenUPP)nil,
  81.             (OSLMarkUPP)nil, 
  82.             (OSLAdjustMarksUPP)nil, 
  83.             (OSLGetErrDescUPP)nil);
  84.     
  85.     err = InstallMyCoercers();
  86.     if ( err ){
  87.         Gripe( "\pInstallMyCoercers failed" );
  88.         return err;
  89.     }
  90.  
  91.     return noErr;
  92. }
  93.  
  94. OSErr TearDownAEObjStuff( void )
  95. {
  96. #ifdef GENERATINGCFM
  97.  
  98.     // We don't want to dispose of the UPP if we are not running
  99.     // under the code fragment manager, as the UPP is an actual
  100.     // pointer to a function in that case.
  101.     
  102.     // OSL Callback functions
  103.  
  104.     if ( gCountUPP )
  105.         DisposeRoutineDescriptor( gCountUPP ); 
  106.     
  107.     if ( gDisposeTokenUPP )
  108.         DisposeRoutineDescriptor( gDisposeTokenUPP );
  109.  
  110.     // Object Accessors
  111.  
  112.     if ( gWindFromNullUPP )
  113.         DisposeRoutineDescriptor( gWindFromNullUPP );
  114.     
  115.     if ( gPropFromWindUPP )
  116.         DisposeRoutineDescriptor( gPropFromWindUPP );
  117.         
  118.     if ( gTextFromWindUPP )
  119.         DisposeRoutineDescriptor( gTextFromWindUPP );
  120.         
  121.     if ( gWordFromTETextUPP )
  122.         DisposeRoutineDescriptor( gWordFromTETextUPP );
  123.         
  124.     if ( gCharFromTETextUPP )
  125.         DisposeRoutineDescriptor( gCharFromTETextUPP );
  126.         
  127.     if ( gPropFromTETextUPP )
  128.         DisposeRoutineDescriptor( gPropFromTETextUPP );
  129.         
  130.     if ( gOspecFromWindUPP )
  131.         DisposeRoutineDescriptor( gOspecFromWindUPP );
  132.     
  133.     // Coercion Handlers
  134.  
  135.     if ( gTextPtrToPStringUPP )
  136.         DisposeRoutineDescriptor( gTextPtrToPStringUPP );
  137.     
  138. #endif
  139.     return noErr;
  140. }
  141.  
  142. OSErr InstallMyAccessors( void )
  143. {
  144.     OSErr err;
  145.     
  146.     /* We install the accessors to get various objects from various containers */
  147.     
  148.     /* Get a Window element from an application (null) container */
  149.     
  150.     gWindFromNullUPP = NewOSLAccessorProc( WindFromNull );
  151.     if ( !gWindFromNullUPP )
  152.         return memFullErr;
  153.  
  154.     err = AEInstallObjectAccessor(cWindow,
  155.                                 typeNull,
  156.                                 (OSLAccessorUPP)gWindFromNullUPP,
  157.                                 0,
  158.                                 false);
  159.  
  160.     /* Get any property from a window container */
  161.     
  162.     gPropFromWindUPP = NewOSLAccessorProc( PropFromWind );
  163.     if ( !gPropFromWindUPP )
  164.         return memFullErr;
  165.  
  166.     err = AEInstallObjectAccessor( typeProperty,
  167.                                 cWindow,
  168.                                 (OSLAccessorUPP)gPropFromWindUPP,
  169.                                 0,
  170.                                 false);
  171.  
  172.     /* Get text from a window container */
  173.     gTextFromWindUPP = NewOSLAccessorProc( TextFromWind );
  174.     if ( !gTextFromWindUPP )
  175.         return memFullErr;
  176.  
  177.     err = AEInstallObjectAccessor( cText,
  178.                                 cWindow,
  179.                                 (OSLAccessorUPP)gTextFromWindUPP,
  180.                                 0,
  181.                                 false);
  182.  
  183.     /* Get a word from a TextEdit text container */
  184.     gWordFromTETextUPP = NewOSLAccessorProc( WordFromTEText );
  185.     if ( !gWordFromTETextUPP )
  186.         return memFullErr;
  187.  
  188.     err = AEInstallObjectAccessor( cWord,
  189.                                 typeTEText,
  190.                                 (OSLAccessorUPP)gWordFromTETextUPP,
  191.                                 0,
  192.                                 false);
  193.  
  194.     /* Get a char (or set thereof) from a TextEdit text container */
  195.     gCharFromTETextUPP = NewOSLAccessorProc( CharFromTEText );
  196.     if ( !gCharFromTETextUPP )
  197.         return memFullErr;
  198.  
  199.     err = AEInstallObjectAccessor( cChar,
  200.                                 typeTEText,
  201.                                 (OSLAccessorUPP)gCharFromTETextUPP,
  202.                                 0,
  203.                                 false);
  204.  
  205.     /* Get any property from a text container */
  206.     gPropFromTETextUPP = NewOSLAccessorProc( PropFromTEText );
  207.     if ( !gPropFromTETextUPP )
  208.         return memFullErr;
  209.  
  210.     err = AEInstallObjectAccessor( typeProperty,
  211.                                 typeTEText,
  212.                                 (OSLAccessorUPP)gPropFromTETextUPP,
  213.                                 0,
  214.                                 false);
  215.  
  216.     /* Get an object specifier from a window container (for Word Services) */
  217.     gOspecFromWindUPP = NewOSLAccessorProc( OspecFromWind );
  218.     if ( !gOspecFromWindUPP )
  219.         return memFullErr;
  220.  
  221.     err = AEInstallObjectAccessor( typeObjectSpecifier,
  222.                                 cWindow,
  223.                                 (OSLAccessorUPP)gOspecFromWindUPP,
  224.                                 0,
  225.                                 false);
  226.  
  227.     return noErr;
  228. }
  229.  
  230. OSErr InstallMyCoercers( void )
  231. {
  232.     OSErr err;
  233.  
  234.     // Note that we're using a pointer, rather than descriptor based coercion
  235.     // function here.  No particular reason why.
  236.  
  237.     gTextPtrToPStringUPP = NewAECoercePtrProc( TextPtrToPString );
  238.     if ( !gTextPtrToPStringUPP )
  239.         return memFullErr;
  240.  
  241.     // I think there is a bug in the Universal Headers, with AECoercePtrUPP vs AECoercionHandlerUPP
  242.  
  243.     err = AEInstallCoercionHandler( typeChar,
  244.                                     typePString,
  245.                                     (void*)(AECoercePtrUPP)gTextPtrToPStringUPP,
  246.                                     0L,
  247.                                     false,        /* Pass a pointer not a descriptor */
  248.                                     false );    /* Application table, not System */
  249.     if ( err ){
  250.         Gripe( "\pAEInstallCoercionHandler failed on TextPtrToPString" );
  251.         return err;
  252.     }
  253.  
  254.     return noErr;
  255. }
  256.  
  257. /* This is a single routine that must know how to count each of the kinds of objects
  258.  * that are used in this application.  Eventually we should have it call functions
  259.  * that are in the ObjXXX files.
  260.  */
  261.  
  262. pascal OSErr MyCountProc(DescType desiredType,
  263.                         DescType containerClass,
  264.                         const AEDesc *container,
  265.                         long *result)
  266. {
  267.     OSErr err = noErr;
  268.     
  269.     switch ( desiredType ){
  270.         case cWindow:
  271.             *result = ( gDocWindow ? 1 : 0 );
  272.             break;
  273.         default:
  274.             Gripe( "\pTrying to count an object I do not know about" );
  275.             err = errAENoSuchObject;
  276.             break;
  277.     }
  278.     
  279.     return err;
  280. }
  281.  
  282. pascal OSErr MyDisposeTokenProc(AEDesc *unneededToken)
  283. {
  284.     OSErr myErr = noErr;
  285.  
  286.     /* In most cases we just toss out the token descriptor.  We may need to do more
  287.      * stuff in the event the the token handle actually points to some real data.
  288.      */
  289.  
  290.     switch (unneededToken->descriptorType) {
  291.         case cWindow:
  292.             myErr = AEDisposeDesc(unneededToken);
  293.             break;
  294.  
  295.         default:
  296.             /* I default to just disposing of the token, ne ces pa  */
  297.             AEDisposeDesc(unneededToken);
  298.             break;
  299.     }
  300.     return(myErr);
  301. }
  302.  
  303. OSErr MakeTextDesc( AEDesc *descPtr )
  304. {
  305.     
  306.     return noErr;
  307. }